import { _decorator, Component, Label, macro, Node, Sprite, SpriteFrame, tween, v3, Vec3 } from 'cc';
import { SkillMgr } from '../manager/SkillMgr';
import { SkillType } from '../enum/SkillType';
import { Debug } from '../util/Debug';
import { UIMgr } from '../manager/UIMgr';
import { UI } from '../enum/UI';
import { EventMgr } from '../manager/EventMgr';
import { EventType } from '../enum/EventType';
import { ResMgr } from '../manager/ResMgr';
import { Bundle } from '../enum/Bundle';
import { CfgSkillInfo } from '../config/CfgSkillInfo';
import { GameMgr } from '../manager/GameMgr';
import { Shake } from '../misc/Shake';
import { Breath } from '../misc/Breath';
const { ccclass, property } = _decorator;

const Tag: string = 'SkillBox'

@ccclass('Game/SkillBox')
export class SkillBox extends Component {

    @property([Label])
    private skillCntTips: Label[] = []
    @property(Breath)
    private tipBreath: Breath = null
    @property(Shake)
    private iconShake: Shake = null

    protected onLoad(): void {
        EventMgr.on(EventType.UpdateSkillCnt, this.onUpdateSkillCnt, this)
        EventMgr.on(EventType.AddSkill, this.onAddSkill, this)
    }

    protected start(): void {
        for (let i = 0; i < this.skillCntTips.length; i++) {
            const skillCntTip: Label = this.skillCntTips[i]
            const cnt: number = SkillMgr.skillCnt(i)
            skillCntTip.string = `${cnt}`
            skillCntTip.node.parent.active = cnt > 0
        }
        this.schedule(this.onCheckStuck, 1, macro.REPEAT_FOREVER)
    }

    protected onDestroy(): void {
        EventMgr.off(EventType.UpdateSkillCnt, this.onUpdateSkillCnt, this)
        EventMgr.off(EventType.AddSkill, this.onAddSkill, this)
    }

    protected onCheckStuck(): void {
        const isStucked: boolean = GameMgr.isStucked
        this.tipBreath.node.active = isStucked
        this.tipBreath.enabled = isStucked
        this.iconShake.enabled = isStucked
    }

    protected onUpdateSkillCnt(skill: SkillType, cnt: number): void {
        const skillCntTip: Label = this.skillCntTips[skill]
        skillCntTip.string = `${cnt}`
        skillCntTip.node.parent.active = cnt > 0
    }

    protected onAddSkill(skill: SkillType): void {
        const flySkillNode: Node = new Node()
        const sp: Sprite = flySkillNode.addComponent(Sprite)
        sp.spriteFrame = ResMgr.getSpriteFrame(Bundle.Icon, CfgSkillInfo[skill].icon)
        this.node.parent.addChild(flySkillNode)
        const btnSkillNode: Node = this.node.children[skill]
        const tw = tween(flySkillNode)
        tw.set({ scale: v3(0, 0, 0) })
        tw.to(0.2, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
        tw.delay(0.1)
        tw.parallel(
            tween(flySkillNode).to(0.5, { worldPosition: btnSkillNode.worldPosition }),
            tween(flySkillNode).to(0.5, { scale: v3(0.5, 0.5, 0.5) }),
        )
        tw.call(() => {
            flySkillNode.destroy()
            tween(btnSkillNode).to(0.2, { scale: v3(1.2, 1.2, 1) }).to(0.2, { scale: v3(1, 1, 1) }).start()
        })
        tw.start()
    }

    protected  onBtnFreezeTimeClick(): void {
        if (SkillMgr.hasSkill(SkillType.FreezeTime)) {
            SkillMgr.useSkill(SkillType.FreezeTime)
        } else {
            UIMgr.open(UI.GetSkill, SkillType.FreezeTime)
            Debug.Warn(Tag, '冰冻道具数量不足，使用失败')
        }
    }

    protected onBtnEraseOneGroupClick(): void {
        if (SkillMgr.hasSkill(SkillType.EraseGroup)) {
            SkillMgr.useSkill(SkillType.EraseGroup)
        } else {
            UIMgr.open(UI.GetSkill, SkillType.EraseGroup)
            Debug.Warn(Tag, '移除道具数量不足，使用失败')
        }
    }

    protected onBtnRefreshPositionClick(): void {
        if (SkillMgr.hasSkill(SkillType.RefreshPosition)) {
            SkillMgr.useSkill(SkillType.RefreshPosition)
        } else {
            UIMgr.open(UI.GetSkill, SkillType.RefreshPosition)
            Debug.Warn(Tag, '刷新位置道具数量不足，使用失败')
        }
    }

    protected onBtnDoubleCoinClick(): void {
        if (SkillMgr.hasSkill(SkillType.DoubleCoin)) {
            SkillMgr.useSkill(SkillType.DoubleCoin)
        } else {
            UIMgr.open(UI.GetSkill, SkillType.DoubleCoin)
            Debug.Warn(Tag, '双倍金币道具数量不足，使用失败')
        }

    }

}


